/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mantechhelpdesk.bean;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import mantechhelpdesk.dao.DbConnection;
import mantechhelpdesk.dao.DepartmentsDao;
import mantechhelpdesk.dao.ThreadsDao;
import mantechhelpdesk.entity.Departments;
import mantechhelpdesk.entity.Threads;

/**
 *
 * @author DinhThang
 */
public class ThreadsBean {

    private Threads threads = null;
    private List<Threads> threadsList;
    private List<SelectItem> departmentsList;
    private DepartmentsDao departmentsDao;
    private ThreadsDao threadsDao;
    private Connection conn;

    public Threads getThreads() {
        return threads;
    }

    public void setThreads(Threads threads) {
        this.threads = threads;
    }

    public List<Threads> getThreadsList() {
        return threadsList;
    }

    public void setThreadsList(List<Threads> threadsList) {
        this.threadsList = threadsList;
    }

    public List<SelectItem> getDepartmentsList() {
        return departmentsList;
    }

    /**
     * Creates a new instance of ThreadsBean
     */

    public ThreadsBean() {
        try {
            threads = new Threads();
            departmentsDao = new DepartmentsDao();
            threadsDao = new ThreadsDao();
            conn = DbConnection.getConnection();
            threadsList = threadsDao.loadAll(conn);

            departmentsList = new ArrayList();
            List allDepart = departmentsDao.loadAll(conn);
            Iterator it = allDepart.iterator();
            while (it.hasNext()) {
                Departments departments = (Departments) it.next();
                departmentsList.add(new SelectItem(departments.getDepartmentID(), departments.getDepartmentName()));
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public String addNew() {
        FacesContext ct = FacesContext.getCurrentInstance();
        try {
            if (threads.getThreadName().length() > 0) {
                Calendar now = GregorianCalendar.getInstance();
                threads.setCreateTime(now.getTime());
                threadsDao.create(conn, threads);
                threadsList = threadsDao.loadAll(conn);
                ct.addMessage("", new FacesMessage("Thread addnew Success!"));
            } else {
                ct.addMessage("", new FacesMessage("Required to input a Thread name!"));
                return "threadAddError";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            ct.addMessage("", new FacesMessage("Cannot addnew Thread!"));
        }
        return "threadAddBack";
    }

    public String update() {
        FacesContext ct = FacesContext.getCurrentInstance();
        try {
            if (threads.getThreadName().length() > 0) {
                threadsDao.save(conn, threads);
                threadsList = threadsDao.loadAll(conn);
                ct.addMessage("", new FacesMessage("Thread update Success!"));
            } else {
                ct.addMessage("", new FacesMessage("Required to input a Thread name!"));
                return "threadUpdateError";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            ct.addMessage("", new FacesMessage("Cannot update Thread!"));
        }
        return "threadUpdateBack";
    }

    public String delete() {
        FacesContext ct = FacesContext.getCurrentInstance();
        try {
            threadsDao.delete(conn, threads);
            ct.addMessage("", new FacesMessage("Thread delete Success!"));
            threadsList = threadsDao.loadAll(conn);
        } catch (Exception ex) {
            ex.printStackTrace();
            ct.addMessage("", new FacesMessage("Cannot delete Thread!"));
        }
        return "threadDelBack";
    }

    public String prepareData4Update() {
        try {
            FacesContext ct = FacesContext.getCurrentInstance();
            Map<String, String> map = ct.getExternalContext().getRequestParameterMap();
            String id = map.get("idParam");
            threads = threadsDao.getObject(conn, Integer.parseInt(id));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "threadUpdate";
    }

    public String prepareData4Delete() {
        try {
            FacesContext ct = FacesContext.getCurrentInstance();
            Map<String, String> map = ct.getExternalContext().getRequestParameterMap();
            String id = map.get("idParam");
            threads = threadsDao.getObject(conn, Integer.parseInt(id));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "threadDel";
    }
}
